package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.MigrateParams;

/**
 * Redis 操作工具类：Database
 * 参考 {@link redis.clients.jedis.commands.DatabaseCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisDatabaseUtils {
    private RedisDatabaseUtils() {
        throw new AssertionError("不能实例化 RedisDatabaseUtils 工具类");
    }

    /**
     * 切换到指定索引(范围是0-15)的数据库。默认情况下，每个新连接都会自动选择到 db0
     *
     * @param index
     * @return java.lang.String
     */
    public static String select(int index) {
        String select;
        try (Jedis jedis = SysJedisPool.getResource()) {
            select = jedis.select(index);
        }
        return select;
    }

    /**
     * 返回当前数据库索引
     *
     * @return int
     */
    public static int getDB() {
        int getDB = -1;
        try (Jedis jedis = SysJedisPool.getResource()) {
            getDB = jedis.getDB();
        }
        return getDB;
    }

    /**
     * 返回当前数据库中 key 的数量
     *
     * @return long
     */
    public static long dbSize() {
        long dbSize;
        try (Jedis jedis = SysJedisPool.getResource()) {
            dbSize = jedis.dbSize();
        }
        return dbSize;
    }

    /**
     * 将指定 key 移动到指定数据库中
     *
     * @param key
     * @param dbIndex
     * @return long
     */
    public static long move(String key, int dbIndex) {
        long move;
        try (Jedis jedis = SysJedisPool.getResource()) {
            move = jedis.move(key, dbIndex);
        }
        return move;
    }

    /**
     * 将指定 key 复制到指定数据库中
     *
     * @param srcKey
     * @param dstKey
     * @param db
     * @param replace
     * @return boolean
     */
    public static boolean copy(String srcKey, String dstKey, int db, boolean replace) {
        boolean copy;
        try (Jedis jedis = SysJedisPool.getResource()) {
            copy = jedis.copy(srcKey, dstKey, db, replace);
        }
        return copy;
    }

    /**
     * 将指定 key 迁移到指定的远程数据库中
     *
     * @param host
     * @param port
     * @param key
     * @param destinationDB
     * @param timeout
     * @return java.lang.String
     */
    public static String migrate(String host, int port, String key, int destinationDB, int timeout) {
        String migrate;
        try (Jedis jedis = SysJedisPool.getResource()) {
            migrate = jedis.migrate(host, port, key, destinationDB, timeout);
        }
        return migrate;
    }

    /**
     * 将指定 key 迁移到指定的远程数据库中
     *
     * @param host
     * @param port
     * @param destinationDB
     * @param timeout
     * @param params
     * @param keys
     * @return java.lang.String
     */
    public static String migrate(String host, int port, int destinationDB, int timeout, MigrateParams params, String... keys) {
        String migrate;
        try (Jedis jedis = SysJedisPool.getResource()) {
            migrate = jedis.migrate(host, port, destinationDB, timeout, params, keys);
        }
        return migrate;
    }

    /*public static void main(String[] args) {
        System.out.println("select：" + select(0));
        System.out.println("getDB：" + getDB());
        System.out.println("dbSize：" + dbSize());

        System.out.println("move：" + move("key-move", 1));
        System.out.println("copy：" + copy("key-copy", "key-copy", 1, true));
        System.out.println("migrate：" + migrate(RedisProperties.HOST, RedisProperties.PORT, "key-migrate", 1, RedisProperties.CONNECTION_TIMEOUT));
        MigrateParams params = MigrateParams.migrateParams();
        params.replace();
        System.out.println("migrate：" + migrate(RedisProperties.HOST, RedisProperties.PORT, 1, RedisProperties.CONNECTION_TIMEOUT,params,"key-migrate1"));
    }*/
}
